﻿using HtmlAgilityPack;

namespace LeekGPTApp.Library.Services;

public class HtmlInfoService : IHtmlInfoService
{
    public async Task<string> getInfoAsync(String id)
    {
        string url = "http://guba.eastmoney.com/list," + id + ".html"; // 要爬取的网页URL
        using (HttpClient client = new HttpClient())
        {
            HttpResponseMessage response =await client.GetAsync(url); // 发送GET请求并获取响应
            String content = "";
            // 检查响应状态码
            if (response.IsSuccessStatusCode)
            {
                string htmlContent = await response.Content.ReadAsStringAsync(); // 获取网页内容
                // 使用HtmlAgilityPack解析网页内容
                var doc = new HtmlDocument();
                doc.LoadHtml(htmlContent);

                // 在解析的HTML中查找特定的元素
                // 这里以查找所有<a>标签为例
                var links = doc.DocumentNode.Descendants("a");

                int index = 0, begin = 9, end = 56, len = 0;
                // 处理找到的链接
                foreach (var link in links)
                {
                    len = content.Length;
                    index++;
                    if (index <= begin || index % 2 == 1)
                    {
                        continue;
                    }
                    if (index > end)
                    {
                        break;
                    }
                    string href = link.GetAttributeValue("href", "");
                    if (href.ToCharArray()[1] == '/')
                    {
                        href = "http:" + href;
                    }
                    else
                    {
                        href = "http://guba.eastmoney.com" + href;
                    }
                    string text = link.InnerText;
                    String s = await getInfopageAsync(href);
                    int tempLen = s.Length;
                    
                    content += s.Substring(3400);
                    content += "*******\n";
                }
            }
            else
            {
                Console.WriteLine("请求失败: " + response.StatusCode);
            }
            return content;
        }
    }

    public async Task<string> getInfopageAsync(String? url)
    {
        using (HttpClient client = new HttpClient())
        {
            HttpResponseMessage response = await client.GetAsync(url); // 发送GET请求并获取响应

            // 检查响应状态码
            if (response.IsSuccessStatusCode)
            {
                string htmlContent = await response.Content.ReadAsStringAsync(); // 获取网页内容

                // 使用HtmlAgilityPack解析网页内容
                var doc = new HtmlDocument();
                doc.LoadHtml(htmlContent);

                // 在解析的HTML中查找特定的元素
                // 这里以查找所有<a>标签为例
                var contents = doc.DocumentNode.Descendants("p");

                String texts = "";

                // 处理找到的链接
                foreach (var content in contents)
                {
                    string text = content.InnerText;
                    if (text.Length > 0 && text.ToCharArray()[0] == '$')
                    {
                        continue;
                    }
                    texts += text;
                }
                if (texts.Length < 40)
                {
                    return "";
                }
                return texts;
            }
            else
            {
                Console.WriteLine("请求失败: " + response.StatusCode);
                return "";
            }
        }
    }
   
}
